package com.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dc.common.ResponseResultVO;
import com.dc.mapper.SysOperLogMapper;
import com.dc.model.SysOperLog;
import com.dc.ro.SysOperLogRO;
import com.dc.service.SysOperLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 操作日志 服务层处理
 */
@Service
public class SysOperLogServiceImpl implements SysOperLogService {

    @Resource
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public ResponseResultVO insertOperLog(SysOperLog operLog) {
        return ResponseResultVO.insertResult(operLogMapper.insert(operLog));
    }

    /**
     * 查询系统操作日志集合
     *
     * @param ro 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public ResponseResultVO selectOperLogList(SysOperLogRO ro) {
        Page page = new Page<Map<String, Object>>(ro.getCurrentPage(), ro.getPageSize());
        LambdaQueryWrapper<SysOperLog> queryWrapper = new QueryWrapper<SysOperLog>().lambda();
        queryWrapper.eq(StringUtils.isNotBlank(ro.getOperName()), SysOperLog::getOperName, ro.getOperName());
        queryWrapper.eq(null != ro.getStatus(), SysOperLog::getStatus, ro.getStatus());
        queryWrapper.ge(StringUtils.isNotBlank(ro.getStartTime()), SysOperLog::getCreateTime, ro.getStartTime());
        queryWrapper.lt(StringUtils.isNotBlank(ro.getEndTime()), SysOperLog::getCreateTime, ro.getEndTime());
        queryWrapper.orderByDesc(SysOperLog::getCreateTime);
        Page result = operLogMapper.selectPage(page, queryWrapper);
        return ResponseResultVO.resultList(result, ro);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param
     * @return 结果
     */
    @Override
    public ResponseResultVO deleteOperLogByIds(String ids) {
        int deleteBatchIds = operLogMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        return ResponseResultVO.deleteResult(deleteBatchIds);
    }


    /**
     * 清空操作日志
     */
    @Override
    public ResponseResultVO cleanOperLog() {
        return ResponseResultVO.deleteResult(operLogMapper.delete(new QueryWrapper<>()));
    }
}
